﻿using Devonline.AspNetCore.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using HttpMethod = Devonline.Core.HttpMethod;
using LogLevel = Devonline.Core.LogLevel;

namespace Devonline.Identity.Admin.Controllers;

/// <summary>
/// 基础数据管理
/// </summary>    
[Route("api/[controller]")]
[ApiController]
[SecurityHeaders]
[Authorize(Roles = GROUP_MAINTAINERS)]
public class ParametersController : ODataModelServiceController<IdentityDbContext, Parameter, ParameterViewModel>
{
    public ParametersController(ILogger<ParametersController> logger, IDataService<IdentityDbContext, Parameter> dataService) : base(logger, dataService) { }

    /// <summary>
    /// 初始化基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("init")]
    public async Task<IActionResult> InitParameter()
    {
        var parameter = await _dataService.GetQueryable().FirstOrDefaultAsync(x => x.Key == "root");
        if (parameter != null)
        {
            return Ok("已经初始化过了!");
        }

        parameter = new Parameter
        {
            Id = "0",
            Index = 0,
            Key = "root",
            Value = "Root",
            Text = "基础数据根节点"
        };
        parameter.Create();
        var list = new List<Parameter> { parameter };
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<DataState>(101));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<DataAccess>(102));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<IdentityType>(103));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AuthorizeType>(104));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AuthType>(105));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<Gender>(106));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<Nation>(107));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AccessLevel>(108));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AllowType>(109));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AuditState>(110));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<ClientType>(111));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<CredentialType>(112));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<DatabaseType>(113, true));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<DataProtectionLevel>(114));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<EncryptionAlgorithm>(115, true));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<EndpointMode>(116));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<EndpointType>(117));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<HashAlgorithm>(118, true));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<HttpMethod>(119));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<LogicType>(120));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<LogLevel>(121));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<MessageType>(122));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<OperatorType>(123));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<PaymentType>(124));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<ProtocolType>(125));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<ResourceType>(126));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<SensitiveWordType>(127));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<SymmetricAlgorithm>(128, true));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<TimeKind>(129));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<TradeType>(130));
        list.AddRange(AspNetCoreExtensions.GetParametersByEnum<AdditionalType>(131));

        foreach (var item in list)
        {
            await _dataService.AddAsync(item, DataServiceContext.GetNoSaveChange());
        }

        await _dataService.SaveChangesAsync();
        return Ok("初始化完成!");
    }
}